package org.scmpatrol.core;

import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;

import org.apache.log4j.Logger;
import org.jboss.annotation.ejb.Depends;
import org.scmpatrol.core.scm.timer.RepositoryUpdateScheduler;
import org.scmpatrol.persistence.SCM;
import org.scmpatrol.scm.SCMRepository;

@Stateless
@Local(value = SCMPatrolManagerLocal.class)
@Depends(value = {
		"jboss.j2ee:module=persistence-0.1-SNAPSHOT.jar,service=EJB3",
		"jboss.j2ee:ear=ear-0.1-SNAPSHOT.ear,jar=core-0.1-SNAPSHOT.jar,name=RepositoryUpdateSchedulerBean,service=EJB3" })
public class SCMPatrolManagerBean implements SCMPatrolManagerLocal {
	private final static Logger LOGGER = Logger
			.getLogger(SCMPatrolManagerBean.class.getName());

	private SCM scm;
	private RepositoryUpdateScheduler scheduler;

	public void startSCMPatrol() {
		LOGGER.info("Starting SCMPatrol...");

		// TODO put this in a dedicated method
		List<SCMRepository> repositories = scm.getRepositories();

		for (SCMRepository repository : repositories) {
			if (LOGGER.isInfoEnabled()) {
				LOGGER.info("Creating scheduler for " + repository);
				scheduler.scheduleRepositoryUpdate(repository);
			}
		}

	}

	@EJB(beanName = "SCMBean")
	public void setSCM(SCM scm) {
		this.scm = scm;
	}

	@EJB(beanName = "RepositoryUpdateSchedulerBean")
	public void setRepositoryUpdater(RepositoryUpdateScheduler scheduler) {
		this.scheduler = scheduler;
	}

}
